From f0c667c16a9cc2a61be017fb0206e21ad23dee60 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Sat, 29 Jan 2022 15:19:10 -0700 Subject: [PATCH] convert tef xml to Format class (#842) * convert tef_xml to Format class. * fix mem leak * cap comment. --- CMakeLists.txt | 1 + GPSBabel.pro | 1 + tef_xml.cc | 128 ++++++++++++++++--------------------------------- tef_xml.h | 113 +++++++++++++++++++++++++++++++++++++++++++ vecs.h | 4 +- 5 files changed, 159 insertions(+), 88 deletions(-) create mode 100644 tef_xml.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 07d53e048..3fd022483 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -261,6 +261,7 @@ set(HEADERS skytraq.h strptime.h subrip.h + tef_xml.h teletype.h unicsv.h units.h diff --git a/GPSBabel.pro b/GPSBabel.pro index 975b01748..dca2de4a4 100644 --- a/GPSBabel.pro +++ b/GPSBabel.pro @@ -248,6 +248,7 @@ HEADERS = \ skytraq.h \ strptime.h \ subrip.h \ + tef_xml.h \ teletype.h \ unicsv.h \ units.h \ diff --git a/tef_xml.cc b/tef_xml.cc index e6a9d5d54..77004f075 100644 --- a/tef_xml.cc +++ b/tef_xml.cc @@ -23,56 +23,28 @@ */ -#include // for QLatin1String -#include // for QString -#include // for QStringView -#include // for QVector -#include // for QXmlStreamAttribute -#include // for QXmlStreamAttributes -#include // for CaseInsensitive - -#include "defs.h" -#include "xmlgeneric.h" // for cb_start, cb_end, xg_callback, xg_string, xg_cb_type, xml_deinit, xml_init, xml_read, xg_tag_mapping - -static Waypoint* wpt_tmp; -static int item_count; -static int waypoints; -static double version; -static route_head* route = nullptr; - -static char* routevia = nullptr; - -static QVector tef_xml_args = { - { - "routevia", &routevia, "Include only via stations in route", - nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr - }, -}; +#include "tef_xml.h" -#define MYNAME "TourExchangeFormat" +#include // for QLatin1String +#include // for QString, QStringView::toString +#include // for QXmlStreamAttribute +#include // for QXmlStreamAttributes +#include // for CaseInsensitive + +#include // for add_const<>::type -static xg_callback tef_start, tef_header, tef_list_start, tef_list_end; -static xg_callback tef_item_start, tef_point, tef_item_end; +#include "defs.h" // for Waypoint, fatal, wp_flags, route_add_head, route_add_wpt, route_head, waypt_add +#include "xmlgeneric.h" // for xg_string, build_xg_tag_map, xml_deinit, xml_init, xml_read -static -xg_tag_mapping tef_xml_map[] = { - { tef_start, cb_start, "/TEF" }, - { tef_header, cb_start, "/TEF/Header" }, - { tef_list_start, cb_start, "/TEF/WaypointList" }, - { tef_item_start, cb_start, "/TEF/WaypointList/Item" }, - { tef_point, cb_start, "/TEF/WaypointList/Item/Point" }, - { tef_item_end, cb_end, "/TEF/WaypointList/Item" }, - { tef_list_end, cb_end, "/TEF/WaypointList" }, - { nullptr, (xg_cb_type)0, nullptr } -}; +#define MYNAME "TourExchangeFormat" /* * tef_start: check for comment "TourExchangeFormat" */ -static void -tef_start(xg_string, const QXmlStreamAttributes* attrv) +void +TefXMLFormat::tef_start(xg_string /*unused*/, const QXmlStreamAttributes* attrv) { bool valid = false; @@ -95,8 +67,8 @@ tef_start(xg_string, const QXmlStreamAttributes* attrv) * tef_header: "Name" > Route name, "Software" > Route descr. */ -static void -tef_header(xg_string, const QXmlStreamAttributes* attrv) +void +TefXMLFormat::tef_header(xg_string /*unused*/, const QXmlStreamAttributes* attrv) { route = new route_head; for (const auto& attr : *attrv) { @@ -109,8 +81,8 @@ tef_header(xg_string, const QXmlStreamAttributes* attrv) route_add_head(route); } -static void -tef_list_start(xg_string, const QXmlStreamAttributes* attrv) +void +TefXMLFormat::tef_list_start(xg_string /*unused*/, const QXmlStreamAttributes* attrv) { if (attrv->hasAttribute("ItemCount")) { item_count = attrv->value("ItemCount").toString().toUInt(); @@ -136,8 +108,8 @@ tef_list_start(xg_string, const QXmlStreamAttributes* attrv) */ // FIXME: the calling convention here is screwy. notes is an input AND // output argument and may be modified. -static char* -fix_notes(const char* name, char* notes) +char* +TefXMLFormat::fix_notes(const char* name, char* notes) { const char* cleft, *cright, *cback; char* ctmp; @@ -170,8 +142,8 @@ fix_notes(const char* name, char* notes) return notes; } -static char* -Xfix_notes(const QString& name, const QString& notes) +char* +TefXMLFormat::Xfix_notes(const QString& name, const QString& notes) { char* cname = xstrdup(name); @@ -184,14 +156,14 @@ Xfix_notes(const QString& name, const QString& notes) return r; } #else -static QString -fix_notes(const QString&, const QString& notes){ +QString +TefXMLFormat::fix_notes(const QString& /*unused*/, const QString& notes){ return notes; } #endif -static void -waypoint_final() +void +TefXMLFormat::waypoint_final() { if (wpt_tmp == nullptr) { return; @@ -225,14 +197,14 @@ waypoint_final() wpt_tmp = nullptr; } -static void -tef_item_end(xg_string, const QXmlStreamAttributes*) +void +TefXMLFormat::tef_item_end(xg_string /*unused*/, const QXmlStreamAttributes* /*unused*/) { waypoint_final(); } -static void -tef_list_end(xg_string, const QXmlStreamAttributes*) +void +TefXMLFormat::tef_list_end(xg_string /*unused*/, const QXmlStreamAttributes* /*unused*/) { waypoint_final(); if (waypoints != item_count) @@ -240,8 +212,8 @@ tef_list_end(xg_string, const QXmlStreamAttributes*) waypoints, item_count); } -static void -tef_item_start(xg_string, const QXmlStreamAttributes* attrv) +void +TefXMLFormat::tef_item_start(xg_string /*unused*/, const QXmlStreamAttributes* attrv) { waypoints++; @@ -272,8 +244,8 @@ tef_item_start(xg_string, const QXmlStreamAttributes* attrv) } } -static double -tef_read_comma_float(QStringView value) +double +TefXMLFormat::tef_read_comma_float(QStringView value) { QString svalue = value.toString(); @@ -286,8 +258,8 @@ tef_read_comma_float(QStringView value) return fixed.toDouble(); } -static void -tef_point(xg_string, const QXmlStreamAttributes* attrv) +void +TefXMLFormat::tef_point(xg_string /*unused*/, const QXmlStreamAttributes* attrv) { if (!wpt_tmp) { return; @@ -302,41 +274,25 @@ tef_point(xg_string, const QXmlStreamAttributes* attrv) } } -static void -tef_xml_rd_init(const QString& fname) +void +TefXMLFormat::rd_init(const QString& fname) { wpt_tmp = nullptr; waypoints = 0; item_count = -1; version = 1.5; - xml_init(fname, tef_xml_map, nullptr); + xml_init(fname, build_xg_tag_map(this, tef_xml_map), nullptr, nullptr, nullptr, true); } -static void -tef_xml_read() +void +TefXMLFormat::read() { xml_read(); } -static void -tef_xml_rd_deinit() +void +TefXMLFormat::rd_deinit() { xml_deinit(); } - -ff_vecs_t tef_xml_vecs = { - ff_type_file, - { ff_cap_none, ff_cap_none, ff_cap_read }, - tef_xml_rd_init, - nullptr, - tef_xml_rd_deinit, - nullptr, - tef_xml_read, - nullptr, - nullptr, - &tef_xml_args, - CET_CHARSET_UTF8, 1 - , NULL_POS_OPS, - nullptr -}; diff --git a/tef_xml.h b/tef_xml.h new file mode 100644 index 000000000..8819beb4a --- /dev/null +++ b/tef_xml.h @@ -0,0 +1,113 @@ +/* + Support for XML based "TourExchangeFormat", + found in Map & Guide Motorrad-Tourenplaner 2005/06 + + Copyright (C) 2005 Olaf Klein, o.b.klein@gpsbabel.org + + Based on kml.c, Keyhole "kml" format. + Copyright (C) 2002-2014 Robert Lipe, robertlipe+source@gpsbabel.org + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +*/ +#ifndef TEF_XML_H_INCLUDED_ +#define TEF_XML_H_INCLUDED_ + +#include // for QList +#include // for QString +#include // for QStringView +#include // for QVector +#include // for QXmlStreamAttributes + +#include "defs.h" // for arglist_t, ff_cap, ff_cap_none, ARGTYPE_BOOL, ARG_NOMINMAX, CET_CHARSET_UTF8, Waypoint, ff_cap_read, ff_type, ff_type_file, route_head +#include "format.h" // for Format +#include "xmlgeneric.h" // for xg_functor_map_entry, xg_string, cb_start, cb_end + + +class TefXMLFormat : public Format +{ +public: + QVector* get_args() override + { + return &tef_xml_args; + } + + ff_type get_type() const override + { + return ff_type_file; + } + + QVector get_cap() const override + { + /* waypoints, tracks, routes */ + return { ff_cap_none, ff_cap_none, ff_cap_read }; + } + + QString get_encode() const override + { + return CET_CHARSET_UTF8; + } + + int get_fixed_encode() const override + { + return 1; + } + + void rd_init(const QString& fname) override; + void read() override; + void rd_deinit() override; + +private: + /* Member Functions */ + + static QString fix_notes(const QString& /*unused*/, const QString& notes); + void waypoint_final(); + static double tef_read_comma_float(QStringView value); + void tef_start(xg_string /*unused*/, const QXmlStreamAttributes* attrv); + void tef_header(xg_string /*unused*/, const QXmlStreamAttributes* attrv); + void tef_list_start(xg_string /*unused*/, const QXmlStreamAttributes* attrv); + void tef_item_end(xg_string /*unused*/, const QXmlStreamAttributes* /*unused*/); + void tef_list_end(xg_string /*unused*/, const QXmlStreamAttributes* /*unused*/); + void tef_item_start(xg_string /*unused*/, const QXmlStreamAttributes* attrv); + void tef_point(xg_string /*unused*/, const QXmlStreamAttributes* attrv); + + /* Data Members */ + + Waypoint* wpt_tmp{}; + int item_count{}; + int waypoints{}; + double version{}; + route_head* route = nullptr; + + char* routevia = nullptr; + + QVector tef_xml_args = { + { + "routevia", &routevia, "Include only via stations in route", + nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr + } + }; + + QList> tef_xml_map = { + { &TefXMLFormat::tef_start, cb_start, "/TEF" }, + { &TefXMLFormat::tef_header, cb_start, "/TEF/Header" }, + { &TefXMLFormat::tef_list_start, cb_start, "/TEF/WaypointList" }, + { &TefXMLFormat::tef_item_start, cb_start, "/TEF/WaypointList/Item" }, + { &TefXMLFormat::tef_point, cb_start, "/TEF/WaypointList/Item/Point" }, + { &TefXMLFormat::tef_item_end, cb_end, "/TEF/WaypointList/Item" }, + { &TefXMLFormat::tef_list_end, cb_end, "/TEF/WaypointList" } + }; +}; +#endif // TEF_XML_H_INCLUDED_ diff --git a/vecs.h b/vecs.h index 5ef98f333..6b00f1275 100644 --- a/vecs.h +++ b/vecs.h @@ -52,6 +52,7 @@ #include "shape.h" #include "skytraq.h" #include "subrip.h" +#include "tef_xml.h" #include "teletype.h" #include "unicsv.h" #include "wintec_tes.h" @@ -90,7 +91,6 @@ extern ff_vecs_t glogbook_vecs; extern ff_vecs_t vcf_vecs; extern ff_vecs_t google_dir_vecs; extern ff_vecs_t tomtom_vecs; -extern ff_vecs_t tef_xml_vecs; extern ff_vecs_t bcr_vecs; extern ff_vecs_t ignr_vecs; extern ff_vecs_t gtm_vecs; @@ -269,7 +269,7 @@ private: LegacyFormat vcf_fmt {vcf_vecs}; LegacyFormat google_dir_fmt {google_dir_vecs}; LegacyFormat tomtom_fmt {tomtom_vecs}; - LegacyFormat tef_xml_fmt {tef_xml_vecs}; + TefXMLFormat tef_xml_fmt; LegacyFormat bcr_fmt {bcr_vecs}; LegacyFormat ignr_fmt {ignr_vecs}; UnicsvFormat unicsv_fmt; -- 2.30.2